﻿using System;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Text;

namespace Astro.Core
{
    public class PagerBuilder
    {
        private class PagerLink
        {
            public string Title { get; set; }
            public int PageNo { get; set; }
            public string Class { get; set; }
        }

        private readonly string _urlTemplate;
        private readonly List<PagerLink> _pagerLinks = new List<PagerLink>();

        public PagerBuilder(string pagerInfo, string urlTemplate)
        {
            _urlTemplate = urlTemplate;
            PageInfo = pagerInfo;

        }

        public string PageInfo { get; set; }
        public string PagerClass { get; set; }

        public void AddPage(string title, int pageNo)
        {
            AddPage(title, pageNo, string.Empty);
        }

        public void AddPage(string title, int pageNo, string itemClass)
        {
            var link = new PagerLink
            {
                PageNo = pageNo,
                Title = title,
                Class = itemClass
            };

            _pagerLinks.Add(link);
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.Append("<nav class=\"pagination\">");

            foreach (var link in _pagerLinks)
            {
                if (link.Class == "first")
                {
                    builder.Append("<span class=\"first\">");
                    builder.AppendFormat("<a href=\"{0}\">&laquo; First</a>", string.Format(_urlTemplate, link.PageNo));
                    builder.Append("</span>");
                }
                else if (link.Class == "end")
                {
                    
                }
                else if (link.Class == "prev")
                {
                    builder.Append("<span class=\"prev\">");
                    builder.AppendFormat("<a href=\"{0}\" rel=\"prev\">&lsaquo; Prev</a>", string.Format(_urlTemplate, link.PageNo));
                    builder.Append("</span>");
                }
                else if (link.Class == "next")
                {
                    builder.Append("<span class=\"next\">");
                    builder.AppendFormat("<a href=\"{0}\" rel=\"next\">Next &rsaquo;</a>", string.Format(_urlTemplate, link.PageNo));
                    builder.Append("</span>");
                }
                else if (link.Class == "last")
                {
                    builder.Append("<span class=\"last\">");
                    builder.AppendFormat("<a href=\"{0}\">Last &raquo;</a>", string.Format(_urlTemplate, link.PageNo));
                    builder.Append("</span>");
                }
                else if (link.Class == "current")
                {
                    builder.AppendFormat("<span class=\"page current\">{0}</span>", link.PageNo);
                }
                else
                {
                    builder.Append("<span class=\"page\">");
                    builder.AppendFormat("<a href=\"{0}\" rel=\"next\">{1}</a>", string.Format(_urlTemplate, link.PageNo), link.PageNo);
                    builder.Append("</span>");
                }
            }

            builder.Append("</nav>");

            return builder.ToString();
        }
    }
}